{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "import pandas as pd\n",
    "import argparse\n",
    "import pymysql\n",
    "\n",
    "sys.path.append(\"/home/jie/.key\")\n",
    "# 本地密码存储文件\n",
    "from sql_key import password\n",
    "\n",
    "connection = pymysql.connect(\n",
    "    host=\"localhost\",  # MySQL数据库的主机\n",
    "    user=\"root\",  # MySQL用户名\n",
    "    password=password,  # MySQL密码\n",
    "    database=\"industry\",  # 你要插入数据的数据库\n",
    "    charset=\"utf8mb4\",\n",
    "    cursorclass=pymysql.cursors.DictCursor,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    # select applicant as company_name, YEAR(application_date) as year, count(*) as cnt from patent_persons \n",
    "    # where applicant=%s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_patent_cnt_by_com_year(company_name):\n",
    "    \"\"\"\n",
    "    根据公司名获取专利数量\n",
    "    \"\"\"\n",
    "    # where applicant=\"{company_name}\" \n",
    "    sql = \"\"\"\n",
    "    select * from patent_p\n",
    "    where applicant=%s\n",
    "    ;\n",
    "    \"\"\".strip()\n",
    "    # group by YEAR(application_date)\n",
    "    # sql = sql.format(company_name=company_name)\n",
    "\n",
    "    with connection.cursor() as cursor:\n",
    "        res = cursor.execute(\n",
    "            sql,\n",
    "            (company_name,)\n",
    "        )\n",
    "        # 提交事务\n",
    "        # connection.commit()\n",
    "        # res = cursor.fetchone()\n",
    "        res = cursor.fetchall()\n",
    "\n",
    "    # if res:\n",
    "    #     return res.get(\"cnt\", None)\n",
    "    \n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "company_name = \"北京大学\"\n",
    "res = get_patent_cnt_by_com_year(company_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12780"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def query_data(sql):\n",
    "    with connection.cursor() as cursor:\n",
    "        res = cursor.execute(sql)\n",
    "        res = cursor.fetchall()\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"\"\"\n",
    "select applicant as company_name, YEAR(application_date) as year, count(*) as cnt, sum(score) from patent_p \n",
    "where applicant='深圳大学'\n",
    "group by YEAR(application_date);\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pprint import pprint "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'cnt': 1783, 'company_name': '深圳大学', 'sum(score)': 1732.25, 'year': 2018},\n",
      " {'cnt': 1673,\n",
      "  'company_name': '深圳大学',\n",
      "  'sum(score)': 1645.333333333333,\n",
      "  'year': 2019},\n",
      " {'cnt': 1396,\n",
      "  'company_name': '深圳大学',\n",
      "  'sum(score)': 1372.9999999999998,\n",
      "  'year': 2020},\n",
      " {'cnt': 997,\n",
      "  'company_name': '深圳大学',\n",
      "  'sum(score)': 960.6666666666667,\n",
      "  'year': 2021},\n",
      " {'cnt': 220, 'company_name': '深圳大学', 'sum(score)': 217.5, 'year': 2013},\n",
      " {'cnt': 204, 'company_name': '深圳大学', 'sum(score)': 201.0, 'year': 2012},\n",
      " {'cnt': 943, 'company_name': '深圳大学', 'sum(score)': 937.0, 'year': 2016},\n",
      " {'cnt': 164, 'company_name': '深圳大学', 'sum(score)': 157.0, 'year': 2022},\n",
      " {'cnt': 1400,\n",
      "  'company_name': '深圳大学',\n",
      "  'sum(score)': 1392.6666666666667,\n",
      "  'year': 2017},\n",
      " {'cnt': 13, 'company_name': '深圳大学', 'sum(score)': 13.0, 'year': 2003},\n",
      " {'cnt': 64, 'company_name': '深圳大学', 'sum(score)': 64.0, 'year': 2008},\n",
      " {'cnt': 107, 'company_name': '深圳大学', 'sum(score)': 106.5, 'year': 2010},\n",
      " {'cnt': 33, 'company_name': '深圳大学', 'sum(score)': 33.0, 'year': 2007},\n",
      " {'cnt': 91, 'company_name': '深圳大学', 'sum(score)': 89.5, 'year': 2009},\n",
      " {'cnt': 32, 'company_name': '深圳大学', 'sum(score)': 30.5, 'year': 2006},\n",
      " {'cnt': 109, 'company_name': '深圳大学', 'sum(score)': 109.0, 'year': 2011},\n",
      " {'cnt': 191,\n",
      "  'company_name': '深圳大学',\n",
      "  'sum(score)': 175.66666666666669,\n",
      "  'year': 2014},\n",
      " {'cnt': 359, 'company_name': '深圳大学', 'sum(score)': 354.0, 'year': 2015},\n",
      " {'cnt': 1, 'company_name': '深圳大学', 'sum(score)': 0.5, 'year': 1989},\n",
      " {'cnt': 30, 'company_name': '深圳大学', 'sum(score)': 30.0, 'year': 2004},\n",
      " {'cnt': 16, 'company_name': '深圳大学', 'sum(score)': 16.0, 'year': 2005},\n",
      " {'cnt': 7, 'company_name': '深圳大学', 'sum(score)': 7.0, 'year': 2002},\n",
      " {'cnt': 2, 'company_name': '深圳大学', 'sum(score)': 2.0, 'year': 2001},\n",
      " {'cnt': 1, 'company_name': '深圳大学', 'sum(score)': 1.0, 'year': 1997}]\n"
     ]
    }
   ],
   "source": [
    "data = query_data(sql)\n",
    "pprint(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"\"\"select applicant as company_name, YEAR(application_date) as year, count(*) as cnt from patent_p \n",
    "where applicant='北京大学'\n",
    "group by YEAR(application_date);\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "select applicant as company_name, YEAR(application_date) as year, count(*) as cnt from patent_p \n",
      "where applicant='北京大学'\n",
      "group by YEAR(application_date);\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "pku_data = query_data(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12780\n"
     ]
    }
   ],
   "source": [
    "cnt = 0\n",
    "for item in pku_data:\n",
    "    cnt += item.get('cnt', 0)\n",
    "print(cnt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "[{'company_name': '北京大学', 'year': 2008, 'cnt': 419},\n",
    " {'company_name': '北京大学', 'year': 2007, 'cnt': 316},\n",
    " {'company_name': '北京大学', 'year': 2009, 'cnt': 472},\n",
    " {'company_name': '北京大学', 'year': 2006, 'cnt': 297},\n",
    " {'company_name': '北京大学', 'year': 2011, 'cnt': 597},\n",
    " {'company_name': '北京大学', 'year': 2013, 'cnt': 534},\n",
    " {'company_name': '北京大学', 'year': 2012, 'cnt': 687},\n",
    " {'company_name': '北京大学', 'year': 2015, 'cnt': 668},\n",
    " {'company_name': '北京大学', 'year': 2018, 'cnt': 1252},\n",
    " {'company_name': '北京大学', 'year': 2019, 'cnt': 1352},\n",
    " {'company_name': '北京大学', 'year': 2020, 'cnt': 1091},\n",
    " {'company_name': '北京大学', 'year': 2021, 'cnt': 708},\n",
    " {'company_name': '北京大学', 'year': 2002, 'cnt': 74},\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pku_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql_2002 = \"\"\"select applicant as company_name, YEAR(application_date) as year, count(*) as cnt from patent_p \n",
    "where applicant='北京大学' and YEAR(application_date)=2002;\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'company_name': '北京大学', 'year': 2002, 'cnt': 74, 'sum(score)': 71.5}]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query_data(sql_2002)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql_2002_ = \"\"\"select * from patent_p \n",
    "where applicant='北京大学' and YEAR(application_date)=2002;\n",
    "\"\"\"\n",
    "per_datas = query_data(sql_2002_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 853566,\n",
       " 'applicant': '北京大学',\n",
       " 'publication_number': 'CN1508284',\n",
       " 'application_date': datetime.date(2002, 12, 20),\n",
       " 'publication_date': datetime.date(2004, 6, 30),\n",
       " 'grant_publication_date': None,\n",
       " 'score': 0.5}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "per_datas[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "71.5\n"
     ]
    }
   ],
   "source": [
    "cnt_ = 0\n",
    "for item in per_datas:\n",
    "    cnt_ += item.get(\"score\", 0)\n",
    "print(cnt_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12780"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 9129,\n",
       " 'applicant': '北京大学',\n",
       " 'publication_number': 'CN101345280',\n",
       " 'application_date': datetime.date(2008, 8, 28),\n",
       " 'publication_date': datetime.date(2009, 1, 14),\n",
       " 'grant_publication_date': None,\n",
       " 'score': 0.5}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymysql.connections.Connection at 0x7ad1ced64100>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"\"\"select applicant as company_name, YEAR(application_date) as year, count(*) as cnt, sum(score) from patent_p \n",
    "where applicant='北京大学'\n",
    "group by YEAR(application_date);\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = query_data(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'company_name': '北京大学', 'year': 1999, 'cnt': 19, 'sum(score)': 18.0}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12004.542857142858\n"
     ]
    }
   ],
   "source": [
    "_cnt = 0\n",
    "for item in data:\n",
    "    _cnt += item.get(\"sum(score)\", 0)\n",
    "print(_cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "38"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "拿到一家公司，每一年的专利数，总的score得分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```sql\n",
    "sql = \"\"\"select applicant as company_name, YEAR(application_date) as year, count(*) as cnt from patent_p \n",
    "where applicant='北京大学'\n",
    "group by YEAR(application_date);\n",
    "\"\"\"\n",
    "```\n",
    "\n",
    "```sql\n",
    "select sum(score) from patent_p where applicant='北京大学';\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = query_data(\"select sum(score) from patent_p where applicant='北京大学';\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'sum(score)': 12004.542857142884}]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = \"\"\"select applicant as company_name, YEAR(application_date) as year, count(*) as cnt, sum(score) from patent_p \n",
    "where applicant='北京大学'\n",
    "group by YEAR(application_date);\n",
    "\"\"\"\n",
    "\n",
    "data = query_data(sql)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12004.542857142858, 12780)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tmp = 0\n",
    "cnt = 0\n",
    "for item in data:\n",
    "    tmp += item.get(\"sum(score)\")\n",
    "    cnt += item.get(\"cnt\")\n",
    "\n",
    "tmp, cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "years = [item[\"year\"] for item in data]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1985, 2022)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(years), max(years)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = list(range(1985, 2024)) + [\"专利件数\", \"专利得分\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = \"/home/jie/gitee/pku_industry/data_process/mysql_industry_process/patent_persons/test.csv\"\n",
    "df = pd.read_csv(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>company_name</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>清华大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>武汉大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  company_name  2016  2017  2018  2019  2020  2021  2022  age\n",
       "0         北京大学  2016  2017  2018  2019  2020  2021  2022   18\n",
       "1         清华大学  2016  2017  2018  2019  2020  2021  2022   18\n",
       "2         武汉大学  2016  2017  2018  2019  2020  2021  2022   18"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_patent_statistics_by_name(name):\n",
    "    if not name:\n",
    "        return {}\n",
    "    \n",
    "    sql = f\"\"\"select applicant as company_name, YEAR(application_date) as year, count(*) as cnt, sum(score) from patent_p \n",
    "    where applicant='{name}'\n",
    "    group by YEAR(application_date);\n",
    "    \"\"\"\n",
    "    with connection.cursor() as cursor:\n",
    "        data = cursor.execute(sql)\n",
    "        data = cursor.fetchall()\n",
    "        \n",
    "    ans = {}\n",
    "    cnt = 0\n",
    "    score = 0\n",
    "    \n",
    "    for k in columns:\n",
    "        ans[k] = None\n",
    "    \n",
    "    for item in data:\n",
    "        cnt += item.get(\"cnt\", 0)\n",
    "        score += item.get(\"sum(score)\", 0)\n",
    "        \n",
    "        year = item.get(\"year\", None)\n",
    "        if year:\n",
    "            ans[year] = item.get(\"cnt\", 0)\n",
    "            \n",
    "        \n",
    "    ans[\"专利得分\"] = score\n",
    "    ans[\"专利件数\"] = cnt\n",
    "    return pd.Series(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_data = get_patent_statistics_by_name(\"北京大学\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "41"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(set(tmp_data.keys()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(tmp_data.keys()) == set(columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "41"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>company_name</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>清华大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>武汉大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  company_name  2016  2017  2018  2019  2020  2021  2022  age\n",
       "0         北京大学  2016  2017  2018  2019  2020  2021  2022   18\n",
       "1         清华大学  2016  2017  2018  2019  2020  2021  2022   18\n",
       "2         武汉大学  2016  2017  2018  2019  2020  2021  2022   18"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[columns] = df['company_name'].apply(get_patent_statistics_by_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>company_name</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>age</th>\n",
       "      <th>1985</th>\n",
       "      <th>...</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "      <th>2020</th>\n",
       "      <th>2021</th>\n",
       "      <th>2022</th>\n",
       "      <th>2023</th>\n",
       "      <th>专利件数</th>\n",
       "      <th>专利得分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>北京大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "      <td>14.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1134.0</td>\n",
       "      <td>1138.0</td>\n",
       "      <td>1252.0</td>\n",
       "      <td>1352.0</td>\n",
       "      <td>1091.0</td>\n",
       "      <td>708.0</td>\n",
       "      <td>211.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12780.0</td>\n",
       "      <td>12004.542857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>清华大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "      <td>172.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3478.0</td>\n",
       "      <td>4697.0</td>\n",
       "      <td>5441.0</td>\n",
       "      <td>5656.0</td>\n",
       "      <td>5199.0</td>\n",
       "      <td>3157.0</td>\n",
       "      <td>699.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>49550.0</td>\n",
       "      <td>46668.958838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>武汉大学</td>\n",
       "      <td>2016</td>\n",
       "      <td>2017</td>\n",
       "      <td>2018</td>\n",
       "      <td>2019</td>\n",
       "      <td>2020</td>\n",
       "      <td>2021</td>\n",
       "      <td>2022</td>\n",
       "      <td>18</td>\n",
       "      <td>42.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1881.0</td>\n",
       "      <td>2496.0</td>\n",
       "      <td>1977.0</td>\n",
       "      <td>2872.0</td>\n",
       "      <td>2677.0</td>\n",
       "      <td>1726.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20603.0</td>\n",
       "      <td>19723.992857</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 50 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "  company_name  2016  2017  2018  2019  2020  2021  2022  age   1985  ...  \\\n",
       "0         北京大学  2016  2017  2018  2019  2020  2021  2022   18   14.0  ...   \n",
       "1         清华大学  2016  2017  2018  2019  2020  2021  2022   18  172.0  ...   \n",
       "2         武汉大学  2016  2017  2018  2019  2020  2021  2022   18   42.0  ...   \n",
       "\n",
       "     2016    2017    2018    2019    2020    2021   2022  2023     专利件数  \\\n",
       "0  1134.0  1138.0  1252.0  1352.0  1091.0   708.0  211.0   NaN  12780.0   \n",
       "1  3478.0  4697.0  5441.0  5656.0  5199.0  3157.0  699.0   NaN  49550.0   \n",
       "2  1881.0  2496.0  1977.0  2872.0  2677.0  1726.0  310.0   NaN  20603.0   \n",
       "\n",
       "           专利得分  \n",
       "0  12004.542857  \n",
       "1  46668.958838  \n",
       "2  19723.992857  \n",
       "\n",
       "[3 rows x 50 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "514.2857142857143"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3600 / 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "connection.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
